

package mx.com.matersys.vacs.web;

import flexjson.JSONSerializer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import mx.com.matersys.vacs.domain.Paciente;
import mx.com.matersys.vacs.domain.RegistroLectura;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RooWebScaffold(path = "pacientes", formBackingObject = Paciente.class)
@RequestMapping("/pacientes")
@Controller
public class PacienteController {

	  @RequestMapping(value = "/consultaUsuario", method = RequestMethod.GET, headers = "Accept=application/json")
	    @ResponseBody
	    public ResponseEntity<java.lang.String> getUsuario() {
	        HttpHeaders headers = new HttpHeaders();
	        
	        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	        String username="init..";
	        if (principal instanceof UserDetails) {
	            username = ((UserDetails)principal).getUsername();
	        } else {
	            username = principal.toString();
	        }
	        System.out.println("PacienteController::especialista---------"+username);
	        
	        String json = new JSONSerializer().exclude("*.class").serialize(principal);
	        return new ResponseEntity<String>(json, headers, HttpStatus.OK);
	    }
	
	
    @RequestMapping(value = "/getPoints", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> getPoints(@RequestParam(value = "idPaciente", required = false) Integer idPaciente, @RequestParam(value = "idLectura", required = false) Integer idLectura) {
        System.out.println("test---------"+idPaciente);
        HttpHeaders headers = new HttpHeaders();
        
        
        String json = new JSONSerializer().exclude("*.class").serialize(recoveryPoints(idPaciente==null?0:idPaciente,idLectura==null?0:idLectura ));
        return new ResponseEntity<String>(json, headers, HttpStatus.OK);
    }

    public List<java.lang.Double> recoveryPoints(int idPaciente,int idLectura) {
    	 List<Double> list = new ArrayList<Double>();
    	if(idPaciente >0 && idLectura > 0 ){
	        
	        
	        RegistroLectura reg = RegistroLectura.findRegistroLectura(idLectura);
	        
	        if(reg != null){
	        	String cad = new String(reg.getRegistro());
	        	System.out.println("cadena: "+cad);
	        
	        	try {
		            BufferedReader br;
		            FileReader f = new FileReader("C:\\bin\\brazalete\\mgh001.art");
		            br = new BufferedReader(f);
		            String cadena = br.readLine();
		            int counter = 0;
		            while (cadena != null) {
		                list.add(Double.parseDouble(cadena));
		                if (counter++ < 0 ) continue;
		                if(counter > 32000) break;
		                cadena = br.readLine();
		            }
		        } catch (Exception ex) {
		            System.out.println("Error " + ex.getMessage());
		        }
		        
	        	
	        }
	        
    	}else{  
	        try {
	            BufferedReader br;
	            FileReader f = new FileReader("C:\\bin\\brazalete\\mgh001.art");
	            br = new BufferedReader(f);
	            String cadena = br.readLine();
	            int counter = 0;
	            while (cadena != null) {
	                list.add(Double.parseDouble(cadena));
	                if (counter++ < 1059777 ) continue;
	                if(counter > 1071777) break;
	                cadena = br.readLine();
	            }
	        } catch (Exception ex) {
	            System.out.println("Error " + ex.getMessage());
	        }
	        
        }
     	
        return list;
    }

    private float getNumberTruncate(float truncate) {
        DecimalFormat df = new DecimalFormat("#0.000");
        return Float.valueOf(df.format(truncate));
    }

    @RequestMapping(value = "/findPacientes", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> findPacientes(@RequestParam(value = "start", required = false) Integer start, 
    													  @RequestParam(value = "limit", required = false) Integer limit, 
    													  @RequestParam(value = "numPaciente", required = false) String numPaciente, 
    													  @RequestParam(value = "nombre", required = false) String nombre, 
    													  @RequestParam(value = "apellidoPaterno", required = false) String apellidoPaterno, 
    													  @RequestParam(value = "apellidoMaterno", required = false) String apellidoMaterno,
    													  @RequestParam(value = "fechaInicial", required = false) String fechaInicial,
    													  @RequestParam(value = "fechaFinal", required = false) String fechaFinal) {
        List<Paciente> listP=null;

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        headers.add("Content-Type", "application/text; charset=utf-8");
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        
        Integer id=0;
        try{
        	id=Integer.parseInt(numPaciente);
        }catch(NumberFormatException e){
        	System.out.println(e.getLocalizedMessage());
        }
        
        try {
			listP = Paciente.findMatchPacientes(id, nombre, apellidoPaterno, apellidoMaterno, fechaInicial!=null && !fechaInicial.equals("")?format.parse(fechaInicial):null, fechaFinal!=null && !fechaFinal.equals("")?format.parse(fechaFinal):null);
		} catch (ParseException e) {
			System.out.println(e.getLocalizedMessage());
	        	
		}
        String json = Paciente.toJsonArray(listP);
        return new ResponseEntity<String>(json, headers, HttpStatus.OK);
    }

    @RequestMapping(value = "/getDetallePaciente", method = RequestMethod.GET, headers = "Accept=application/json")
   // @ResponseBody
    public ResponseEntity<java.lang.String> getDetallePaciente(@RequestParam(value = "idPaciente", required = true) Integer idPaciente) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        headers.add("Content-Type", "application/text; charset=utf-8");
      //  System.out.println("idPaciente: " + idPaciente);
        return new ResponseEntity<String>(Paciente.findPaciente(idPaciente).toJson(), headers, HttpStatus.OK);
    }
    
    
    
}

